<!--
 * @Author: wangming
 * @Date: 2020-09-09 14:04:55
 * @LastEditors: wangming
 * @LastEditTime: 2021-09-10 09:50:46
 * @Description:添加类别按钮
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="660px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-middle pl15 pr15">
        <span>新建规则</span>
        <span class="f12 g9 ks-col ml10"
          >请先添加单选、多选、下拉选择，再来设置关联显示条件</span
        >
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="FormData"
      inline
      size="mini"
      ref="Form"
      label-position="left"
    >
      <el-form-item
        prop="label"
        label="规则名称"
        :rules="[
          { required: true, message: '请输入规则名称', trigger: 'blur' },
        ]"
      >
        <el-input
          v-model="FormData.label"
          placeholder="请输入规则名称"
        ></el-input>
      </el-form-item>
      <div class="ks-row f12 g9 mb10">
        <div class="max ml20">变量</div>
        <div class="w100 pl10">运算符</div>
        <div class="max pl10">值</div>
      </div>
      <div v-for="(item, index) in FormData.rules" :key="index">
        <div class="ks-row-top lhs">
          <div class="f12 tc g3 mr5">
            {{ index > 0 ? "且" : "当" }}
          </div>
          <el-form-item
            :prop="'rules.' + index + '.modelName'"
            :rules="{
              required: true,
              message: '关联控件不能为空',
              trigger: 'blur',
            }"
          >
            <el-select
              class="max"
              v-model="item.modelName"
              placeholder="请选择关联控件"
              value-key="value"
            >
              <el-option
                :label="item.__config.label"
                :value="item.modelName"
                v-for="(item, index) in originModelList"
                :key="index"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="w100">
            <el-select
              v-model="item.checked"
              placeholder="请选择"
              value-key="value"
            >
              <el-option label="等于" :value="true"> </el-option>
              <el-option label="不等于" :value="false"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            :prop="'rules.' + index + '.value'"
            :rules="{
              required: true,
              message: '值不能为空',
              trigger: 'blur',
            }"
          >
            <el-select
              class="max"
              v-model="item.value"
              placeholder="请选择值"
              value-key="value"
            >
              <el-option
                :label="item.label"
                :value="`[${item.id}]`"
                v-for="(item, index) in options[item.modelName]"
                :key="index"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-button
            type="text"
            icon="el-icon-delete"
            @click="FormData.rules.splice(index, 1)"
          ></el-button>
        </div>
      </div>
    </el-form>
    <div class="pl20 mt20">
      <div
        class="addbtn ks-row-middle-center lh1 poi mt10"
        @click="addCondition"
        v-show="originModelList.length > 0"
      >
        <span class="el-icon-plus"></span>
        <span>添加条件组</span>
      </div>
    </div>
    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { cloneDeep } from "lodash";
import { postHmhttprequestdefinitionExecute } from '@/apis/paas/HmHttpRequestDefinitionController.js'
export default {
  data() {
    return {
      originModelList: [],
      options: {},
      FormData: {
        label: "",
        rules: [],
      },
      showVisible: false,
    };
  },
  methods: {
    async getOptions(
      { modelKey, relationType, valueTeam, modelCode },
      modelName
    ) {
      let data = await postHmhttprequestdefinitionExecute({
        key: "selectList",
        modelKey,
        values: {
          _RELATION_TYPE_: relationType,
          value: valueTeam,
          modelCode,
        },
      });
      this.$set(this.options, modelName, data);
    },
    addCondition() {
      this.FormData.rules.push({ modelName: "", value: "", checked: true });
    },
    editForm(FormData = {}, originModelList) {
      this.FormData = cloneDeep(FormData);
      this.originModelList = originModelList;
      return this.showModel();
    },
    addForm(originModelList) {
      this.originModelList = originModelList;
      this.FormData = {
        label: "",
        rules: [],
      };
      return this.showModel();
    },
    showModel() {
      this.originModelList.forEach((config) => {
        let { modelKey, relationType, valueTeam, modelCode } = config.__config;
        this.getOptions(
          { modelKey, relationType, valueTeam, modelCode },
          config.modelName
        );
      });
      this.showVisible = true;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.Form.validate();
        this.showVisible = false;
        this.options = {};
        this.callback(this.FormData);
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped>
.max {
  width: 220px;
}
.addbtn {
  width: 140px;
  height: 32px;
  border-radius: 4px;
  border: 1px dashed #1890ff;
  color: #1890ff;
}
.lhs {
  line-height: 28px;
}
</style>
